import moment from "moment";
import { message } from "antd";
import { messages } from "share/common";

export default {
  getRelationTypeConfig(relationType) {
    let config = null;
    switch (relationType) {
      case "5001":
        config = {
          type: "user",
          valueKey: "id",
          labelKey: "fullName",
          onlyNeed: "id",
          isNeedExtraProcess: false, //是否需要额外处理listExtraParams
          listExtraParams: { roleType: "TENANT" },
        };
        break;
      case "9002":
        config = {
          type: "select_bank",
          valueKey: "id",
          labelKey: "bankBranchName",
          onlyNeed: "id",
        };
        break;
      case "9003":
        config = {
          type: "vendor_account_no_type",
          valueKey: "id",
          labelKey: "venNickname",
          onlyNeed: "id",
          isNeedExtraProcess: true,
          listExtraParams: { companyOid: "" },
        };
        break;
    }
    return config;
  },

  //找相关联的引用字段
  findFieldsByFieldOID(fieldOID, fields) {
    let result = [];
    let linkageFieldCodeFixList = [];
    let linkageFieldCodeCustomList = [];
    fields &&
      fields.forEach((field) => {
        let dataSource = field.dataSource ? JSON.parse(field.dataSource) : {};
        if (dataSource.linkageFieldOID === fieldOID) {
          if (dataSource.linkageDataType === "fix") {
            linkageFieldCodeFixList.addIfNotExist(dataSource.linkageFieldCode);
          }
          if (dataSource.linkageDataType === "custom") {
            linkageFieldCodeCustomList.addIfNotExist(dataSource.linkageFieldCode);
          }
          result.push(field);
        }
      });
    return { result, linkageFieldCodeFixList, linkageFieldCodeCustomList };
  },

  //处理要更新的引用字段数据
  getSetData(relationFields, dataList, formKey) {
    let setData = {};
    relationFields &&
      relationFields.forEach((field) => {
        let dataSource = field.dataSource ? JSON.parse(field.dataSource) : {};
        let valueItem = dataList.find((item) => {
          return item.dataType === dataSource.linkageDataType && item.fieldCode === dataSource.linkageFieldCode;
        });
        let value = valueItem ? valueItem.value : undefined;
        if (value && dataSource.fieldTypeId === "105") {
          value = moment(value);
        }
        setData[field[formKey]] = value;
      });
    return setData;
  },
};
